/**
 * 
 */
package sailing.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.Customer;
import sailing.listener.DATA;
import sailing.listener.DataListener;
import sailing.listener.UI;
import sailing.managers.CustomerManager;
import sailing.resource.Resource;
import sailing.ui.base.ITabPanel;
import sailing.ui.base.TopTitlePanel;
import sailing.ui.model.CustomerTableModel;
import sailing.ui.model.EditBtnCellEditor;
import sailing.ui.model.EditBtnCellRenderer;
import sailing.ui.systemsetting.AccountingSubjectMgtPanel;
import sailing.ui.systemsetting.BankAccountMgtPanel;
import sailing.ui.systemsetting.CashAccountMgtPanel;
import sailing.ui.systemsetting.CashierMgtPanel;
import sailing.ui.systemsetting.NewCustomerDialog;
import cjlite.logger.Logger;
import cjlite.utils.Strings;

/**
 * @author kevin
 * 
 */
public class SystemSettingPanel extends AbstractJPanel implements ITabPanel {
	private static Logger logger = Logger.thisClass();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//
	JTabbedPane tabbedpane;
	//
	private static final Color titleBgColor = new Color(183, 219, 255);

	private JTable customerMgtTable;
	private JButton addNewCustomerBtn;

	//
	private CustomerTableModel customerTableModel;
	//
	private DataListener dataListener = new DataListener() {

		@Override
		public void dataChanged() {
			refreshDataModel();
		}
	};

	/**
	 * @param application
	 */
	public SystemSettingPanel(SingleFrameApplication application) {
		super(application);
		DATA.addDataListener(dataListener);
	}

	protected void refreshDataModel() {
		if (customerTableModel != null) {
			customerTableModel.fireTableDataChanged();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
		this.setLayout(new BorderLayout());
		tabbedpane = new JTabbedPane(JTabbedPane.LEFT);
		tabbedpane.setBorder(new EmptyBorder(5, 5, 5, 5));
		tabbedpane.addTab("客户管理", this.getCustomerMgtPanel());
		tabbedpane.addTab("会计科目管理", this.getAccountingSubjectMgtPanel());
		tabbedpane.addTab("银行及账户管理", this.getBankAccountMgtPanel());
		tabbedpane.addTab("库存现金管理", this.getCashMgtPanel());
		tabbedpane.addTab("经手人管理", this.getCashierMgtPanel());
		this.add(tabbedpane, BorderLayout.CENTER);
	}



	private JPanel getCashMgtPanel() {
		return new CashAccountMgtPanel(getApplication());
	}

	private JPanel getAccountingSubjectMgtPanel() {
		return new AccountingSubjectMgtPanel(getApplication());
	}

	private JPanel getBankAccountMgtPanel() {
		return new BankAccountMgtPanel(getApplication());
	}

	private JPanel getCashierMgtPanel() {
		return new CashierMgtPanel(getApplication());
	}

	@Override
	public String getTabName() {
		return "系统设置";
	}

	@Override
	public JPanel getContentPanel() {
		return this;
	}

	private JPanel getCustomerMgtPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 0));
		panel.setLayout(new BorderLayout(10, 10));
		//panel.add(this.getCustomerMgtTopPanel(), BorderLayout.NORTH);
		panel.add(new TopTitlePanel("客户管理"), BorderLayout.NORTH);
		
		
		customerMgtTable = new JTable();
		customerTableModel = new CustomerTableModel(this.getApplication());
		customerMgtTable.setModel(customerTableModel);
		customerMgtTable.setRowHeight(35);
		customerMgtTable.setIntercellSpacing(new Dimension(15, 5));
		customerTableModel.updateModel();
		JScrollPane scrollpane = new JScrollPane(customerMgtTable);
		// scrollpane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.add(scrollpane, BorderLayout.CENTER);
		panel.add(this.CustomerMgtSidebarPanel(), BorderLayout.EAST);

		return panel;
	}

	private JPanel CustomerMgtSidebarPanel() {
		JPanel panel = new JPanel();
		addNewCustomerBtn = new JButton();
		addNewCustomerBtn.setText("添加新客户");
		addNewCustomerBtn.setMargin(new Insets(5, 30, 5, 30));
		addNewCustomerBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JDialog newCustomerDialog = new NewCustomerDialog(getApplication().getMainFrame(), getApplication());
				newCustomerDialog.setVisible(true);
			}
		});

		JCheckBox showhideEditBtn = new JCheckBox();
		showhideEditBtn.setText("显示隐藏编辑按钮");

		showhideEditBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JCheckBox cb = (JCheckBox) e.getSource();
				logger.trace("checkbox is selected[{0}]", cb.isSelected());
				showHideEditBtn(cb.isSelected());
			}
		});

		Box bv = Box.createVerticalBox();
		bv.add(addNewCustomerBtn);
		bv.add(Box.createVerticalStrut(20));
		bv.add(showhideEditBtn);
		panel.add(bv);
		return panel;
	}

	protected void showHideEditBtn(boolean selected) {
		this.customerTableModel.showEditBtn(selected);
		// this.customerTableModel.fireTableDataChanged();
		this.customerTableModel.fireTableStructureChanged();
		if (selected) {
			this.customerMgtTable.getColumnModel().getColumn(2).setCellRenderer(new EditBtnCellRenderer("编辑", "删除"));
			EditBtnCellEditor editBtnCell = new EditBtnCellEditor("编辑", "删除");
			editBtnCell.addBtn1Listener(editAL);
			editBtnCell.addBtn2Listener(deleteAL);
			this.customerMgtTable.getColumnModel().getColumn(2).setCellEditor(editBtnCell);
		}
	}

	ActionListener editAL = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() instanceof EditBtnCellEditor) {
				EditBtnCellEditor ce = (EditBtnCellEditor) e.getSource();
				if (ce.getCellEditorValue() instanceof Customer) {
					Customer cc = (Customer) ce.getCellEditorValue();
					logger.trace("edit {0}", cc.getName());
					NewCustomerDialog newCustomerDialog = new NewCustomerDialog(getApplication().getMainFrame(),
							getApplication());
					newCustomerDialog.setEditCustomer(cc);
					newCustomerDialog.setVisible(true);
				}
			}
		}
	};

	ActionListener deleteAL = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() instanceof EditBtnCellEditor) {
				EditBtnCellEditor ce = (EditBtnCellEditor) e.getSource();
				if (ce.getCellEditorValue() instanceof Customer) {
					Customer cc = (Customer) ce.getCellEditorValue();
					logger.trace("delete {0}", cc.getName());
					int result = JOptionPane.showConfirmDialog(SystemSettingPanel.this,
							Strings.concat("确定要删除：'", cc.getName(), "' ?"), "提示", JOptionPane.YES_NO_OPTION);
					if (JOptionPane.YES_OPTION == result) {
						CustomerManager cm = SystemSettingPanel.this.getApplication().getContext().getIoc()
								.getInstance(CustomerManager.class);

						cm.delete(cc);
						UI.refresh();
					}
				}
			}
		}
	};

	private JPanel getCustomerMgtTopPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEtchedBorder());
		panel.setBackground(titleBgColor);
		panel.setOpaque(true);
		panel.add(new JLabel("客户管理"));
		return panel;
	}

	@Override
	public Icon getTabIcon() {
		return Resource.getImageIcon("/ui/maintop/systemsetting.24.png");
	}

	@Override
	public String getTabTip() {
		return null;
	}

}
